package ua.org.extjsbuilder.parser.scanner;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import junit.framework.TestCase;
import ua.org.extjsbuilder.parser.ITokenList;
import ua.org.extjsbuilder.parser.JSParseException;
import ua.org.extjsbuilder.parser.runtime.JSGlobalToken;

public class CodeBlocksScannerTestCase extends TestCase {
	private static final String MOCK_PKG = "ua/org/extjsbuilder/parser/mock/";
	public void testScan() throws Exception{
		successScan("test7.js");
		failureScan("test8.js");
		failureScan("test9.js");
	}
	
	private void failureScan (String resource) throws IOException {
		try{
			successScan(resource);
			fail("This scan should fail");
		}
		catch(JSParseException ex){
			ex.printStackTrace();
		}
	}

	private void successScan(String resource) throws IOException {
		ITokenList global = getGlobalFor(resource);
		global.scan(new LinesScanner());
		global.scan(new CommentsAndLiteralsScanner());
		global.scan(new CodeBlocksScanner());
	}

	private ITokenList getGlobalFor(String resource) throws IOException{
		return new JSGlobalToken(new InputStreamReader(
				getStream(resource)));
	}
	
	private InputStream getStream(String resource) {
		return getClass().getClassLoader()
			.getResourceAsStream(MOCK_PKG+resource);
	}
}
